<?php
defined('BASEPATH') OR exit('No direct script access allowed');

function msubstr($str, $length = 180, $suffix = FALSE, $start = 0, $charset = 'utf-8') {
	$str = str_replace(array("\r\n", "\n\r", "\n", "\r", "\t", '&nbsp;', '  '), '', strip_tags($str));

    if (function_exists("mb_substr")) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if (FALSE === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice.'...' : $slice;
}

/**
 * [unfile description]
 * @param  string $file [description]
 * @param  [type] $path [description]
 * @return [type]       [description]
 */
function unfile($file = '', $path = '') {
	if (empty($file)) {
		return FALSE;
	}

	is_array($file) OR $file = [$file];

	$path = rtrim(str_replace('\\', '/', FCPATH), '/') .'/'. $path;

	foreach ($file as $key => $value) {
		if (is_string($value)) {
			file_exists($path . $value) AND unlink($path . $value);
		} else {
			foreach ($value as $key => $val) {
				file_exists($path . $val) AND unlink($path . $val);
			}
		}
	}

	return TRUE;
}

/**
 * [user_password description]
 * @param  string $password [description]
 * @return [type]           [description]
 */
function user_password($password = '') {
	if (empty($password)) {
		return '';
	}
	return md5(md5($password) . 'www.ziyunbaixing.com');
}

/**
 * [member_password description]
 * @param  string $password [description]
 * @return [type]           [description]
 */
function member_password($password = '') {
	if (empty($password)) {
		return '';
	}
	return md5(md5($password) . 'ziyunbaixing.com');
}

/**
 * [p description]
 * @param  string $data [description]
 * @return [type]       [description]
 */
function p($data = '') {
	if (empty($data)) {
		return NULL;
	}
	echo '<pre>';
	print_r($data);
	echo '</pre>';
}
